﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{   
    [Serializable]
    public sealed class Project{
        private ProjectDetails projectDetails;
        private List<string> psiList = new List<string>();
        private List<PID> pidList = new List<PID>();
        private List<WorkNode> workNodeList = new List<WorkNode>();
        private RiskMatrix riskMatrix;
        private List<HazopTeam> hazopTeamList = new List<HazopTeam>();
        private List<GeneralAdviceItem> _generalAdvices = new List<GeneralAdviceItem>();
        private static Project projectInstance = null;
        private static object syncObj = new object();

        private Project(){}


        public static Project ProjectInstance
        {
            get
            {
                if (projectInstance == null)
                {
                    lock (syncObj)
                    {
                        if (projectInstance == null)
                            projectInstance = new Project();
                    }
                }
                return projectInstance;
            }
        }

        public int GetTotalAdviceCount()
        {
            int count = 0;
            foreach (WorkNode node in workNodeList)
            {
                foreach (WorkNodeItem item in node.WorkNodeItemList)
                {
                    if (item.Recommendations != null)
                    {
                        count++;
                    }
                }
            }
            return count;
        }

        public List<WorkNodeItem> GetAdviceWorkNodeItem() {
            List<WorkNodeItem> res = new List<WorkNodeItem>();
            foreach (WorkNode node in workNodeList) {
                foreach (WorkNodeItem item in node.WorkNodeItemList) {
                    if (item.Recommendations != null) {
                        res.Add(item);
                    }
                }
            }
            return res;
        }

        public int GetGeneralAdvice(){
            int count = 1;
            foreach (GeneralAdviceItem item in GeneralAdvices){
                if(!string.IsNullOrEmpty(item.AdviceNum)){
                    count++;
                }
            }
            return count;
        }

        public Project(ProjectDetails projectDetails, List<string> psiList, List<PID> pidList, List<WorkNode> workNodeList, RiskMatrix riskMatrix, List<HazopTeam> hazopTeam, List<GeneralAdviceItem> generalAdvices)
        {
            this.projectDetails = projectDetails;
            this.psiList = psiList;
            this.pidList = pidList;
            this.workNodeList = workNodeList;
            this.riskMatrix = riskMatrix;
            this.hazopTeamList = hazopTeam;
            this.GeneralAdvices = generalAdvices;
        }

        public ProjectDetails ProjectDetails{
            get { return projectDetails; }
            set { projectDetails = value; }
        }

        public List<string> PsiList{
            get { return psiList; }
            set { psiList = value; }
        }

        public List<PID> PidList{
            get { return pidList; }
            set { pidList = value; }
        }

        public List<WorkNode> WorkNodeList{
            get { return workNodeList; }
            set { workNodeList = value; }
        }

        public RiskMatrix RiskMatrix{
            get { return riskMatrix; }
            set { riskMatrix = value; }
        }

        public List<HazopTeam> HazopTeam
        {
            get { return hazopTeamList; }
            set { hazopTeamList = value; }
        }

        public List<GeneralAdviceItem> GeneralAdvices{
            get { return _generalAdvices; }
            set { _generalAdvices = value; }
        }

        public override bool Equals(object obj){
            if (obj != null && obj is Project){
                Project p = (Project) obj;
                if(this.projectDetails == p.projectDetails && this.riskMatrix == p.riskMatrix){
                    foreach (string s in this.psiList){
                        if (p.psiList.Contains(s)){
                            continue;
                        }
                        else{
                            return false;
                        }
                    }
                    for (int i = 0; i < this.pidList.Count; i++){
                        if (pidList[i].Equals(p.pidList[i])){
                            continue;
                        }
                        else{
                            return false;
                        }
                    }
                    for (int i = 0; i < this.workNodeList.Count; i++){
                        if (this.workNodeList[i].Equals(p.workNodeList[i])){
                            continue;
                        }
                        else{
                            return false;
                        }
                    }
                    for (int i = 0; i < this.hazopTeamList.Count; i++){
                        if (this.hazopTeamList[i].Equals(p.hazopTeamList[i])){
                            continue;
                        }
                        else{
                            return false;
                        }
                    }
                    for (int i = 0; i < this.GeneralAdvices.Count; i++){
                        if(this.GeneralAdvices[i].Equals(p.GeneralAdvices[i])){
                            continue;
                        }
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }
    }
}
